package com.tsugun.mediaplayer.view.widget;

import android.content.Context;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewCompat;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;

/**
 * 自定义滑动控件
 * Created by shize on 2017/8/29.
 */

public class DragViewGroup extends FrameLayout {

    private static final int DISTANCE_MOVE_MIN = 30; // 判断误触的距离

    private ViewDragHelper mViewDragHelper; // 滑动工具类
    private View mFirstView; // 第一个 view
    private View mSecondView; // 第二个 view
    private int mFirstViewWidth;

    private boolean isTarget = false; // 是否滑动到指定条件
    private DragCallBack mDragCallBack; // 滑动事件回调

    public DragViewGroup(@NonNull Context context) {
        this(context, null);
    }

    public DragViewGroup(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DragViewGroup(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * 布局加载完成后调用
     * 在这个方法里获取子view
     */
    @Override
    protected void onFinishInflate() {
        mViewDragHelper = ViewDragHelper.create(this, 1.0f, callback);
        super.onFinishInflate();
        mFirstView = getChildAt(0);
        mSecondView = getChildAt(1);
    }

    /**
     * 子view的大小改变后回调该方法
     * 在这个方法里获取到mFirstView的宽度
     */
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mFirstViewWidth = mFirstView.getMeasuredWidth();
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        //注意：如果需要滑动，一定要将触摸事件拦截下来
        return true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        //必须将触摸事件传递给mDragHelper
        mViewDragHelper.processTouchEvent(ev);
        return true;
    }

    @Override
    public void computeScroll() {
        if (mViewDragHelper.continueSettling(true)) {
            ViewCompat.postInvalidateOnAnimation(this);
        }
    }

    private ViewDragHelper.Callback callback = new ViewDragHelper.Callback() {

        @Override
        public boolean tryCaptureView(View child, int pointerId) {
            return mSecondView == child;
        }

        @Override
        public int clampViewPositionVertical(View child, int top, int dy) {
            return 0;
        }

        @Override
        public int clampViewPositionHorizontal(View child, int left, int dx) {
            return left;
        }

//        @Override
//        public int getViewHorizontalDragRange(View child) {
//            return getMeasuredWidth() - child.getMeasuredWidth();
//        }
//
//        @Override
//        public int getViewVerticalDragRange(View child) {
//            return getMeasuredHeight() - child.getMeasuredHeight();
//        }

        @Override
        public void onViewReleased(View releasedChild, float xvel, float yvel) {
            super.onViewReleased(releasedChild, xvel, yvel);
            if (mSecondView.getLeft() < 500) {
                isTarget = false;
                scrollOpen();
            } else {
                isTarget = true;
                scrollClose();
            }
        }

        @Override
        public void onViewDragStateChanged(int state) {
            super.onViewDragStateChanged(state);
            switch (state) {
                case ViewDragHelper.STATE_IDLE: // 闲置 0
                    if (mDragCallBack != null) {
                        mDragCallBack.onSettlingComplete(isTarget);
                    }
                    break;
                case ViewDragHelper.STATE_DRAGGING: // 拖动 1
                    break;
                case ViewDragHelper.STATE_SETTLING: // 动画沉降 2
                    break;
            }
        }
    };

    /**
     * 达到条件，移除第二个 view
     */
    private void scrollClose() {
        //显示第一个view
        mViewDragHelper.smoothSlideViewTo(mSecondView, mFirstViewWidth, 0);
        ViewCompat.postInvalidateOnAnimation(DragViewGroup.this);
    }

    /**
     * 未达到条件，还原第二个 view
     */
    private void scrollOpen() {
        //如果第二个view的left小于500像素，就不显示第一个view
        mViewDragHelper.smoothSlideViewTo(mSecondView, 0, 0);
        ViewCompat.postInvalidateOnAnimation(DragViewGroup.this);
    }

    public void setDragCallBack(DragCallBack dragCallBack) {
        mDragCallBack = dragCallBack;
    }

    public interface DragCallBack {
        /**
         * 当沉降结束
         *
         * @param isTarget 是否到达指定目标
         */
        void onSettlingComplete(boolean isTarget);
    }
}
